package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sikaryofficial.backend.domain.dto.resp.AddressUserListDTO;
import com.sikaryofficial.backend.domain.entity.Address;
import com.sikaryofficial.backend.domain.entity.UserAddress;
import com.sikaryofficial.backend.domain.mapping.AddressMapping;
import com.sikaryofficial.backend.domain.mapping.UserAddressMapping;
import com.sikaryofficial.backend.mapper.AddressMapper;
import com.sikaryofficial.backend.mapper.UserAddressMapper;
import com.sikaryofficial.backend.service.IUserAddressService;
import com.sikaryofficial.system.api.model.AddressDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 我的地址Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {
    @Autowired
    private UserAddressMapper addressUserMapper;
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<AddressUserListDTO> getDefaultAddressList(List<Long> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        // 查询默认地址
        List<UserAddress> userAddressList = addressUserMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getDeletedVersion, 0L)
                .eq(UserAddress::getAddressDefault, 0)
                .in(UserAddress::getCreatedBy, userIds)
        );
        List<AddressUserListDTO> addressUserListDTOS = UserAddressMapping.INSTANCE.coverListToResp(userAddressList);
        if (CollUtil.isEmpty(addressUserListDTOS)) {
            return Lists.newArrayList();
        }
        List<Long> addressIds = addressUserListDTOS.stream().map(AddressUserListDTO::getAddressId).collect(Collectors.toList());
        List<Address> addressList = addressMapper.selectList(new LambdaQueryWrapper<Address>()
                .eq(Address::getDeletedVersion, 0L)
                .in(Address::getAddressId, addressIds)
        );
        List<AddressDTO> addressDTOList = AddressMapping.INSTANCE.coverListToResp(addressList);
        // 构建map集合
        Map<Long, AddressDTO> addressDTOMap =
                addressDTOList.stream().collect(Collectors.toMap(AddressDTO::getAddressId, Function.identity(), (k1, k2) -> k1));
        // 填充DTO中地址相关字段
        addressUserListDTOS.forEach(item -> {
            AddressDTO addressDTO = addressDTOMap.get(item.getAddressId());
            Optional.ofNullable(addressDTO).ifPresent(item::setAddress);
        });
        return addressUserListDTOS;
    }

    /**
     * 获取用户地址map集合
     *
     * @param userAddressIds
     * @return
     */
    @Override
    public Map<Long, AddressUserListDTO> getAddressListByIds(List<Long> userAddressIds) {
        if (CollUtil.isEmpty(userAddressIds)) {
            return Maps.newHashMap();
        }
        List<UserAddress> userAddressList = addressUserMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .in(UserAddress::getUserAddressId, userAddressIds)
        );
        List<AddressUserListDTO> addressUserListDTOS = UserAddressMapping.INSTANCE.coverListToResp(userAddressList);
        if (CollUtil.isEmpty(addressUserListDTOS)) {
            return Maps.newHashMap();
        }
        return addressUserListDTOS.stream().collect(Collectors.toMap(AddressUserListDTO::getUserAddressId, Function.identity(), (k1, k2) -> k1));
    }
}
